/**
 * 
 */
package demo.feed;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author roger.xu
 * 
 */
public class AtomSaxContentHandler extends DefaultHandler {

    private static Log log = LogFactory.getLog(AtomSaxContentHandler.class.getName());

    enum ElementTag {
        ENTRY("entry"), ID("id"), TITLE("title"), CATEGORY("category"), UPDATED("updated"), LINK("link"), CONTENT(
                "content"), AUTHOR("author"), EMAIL("email"), IN_REPLY_TO("thr:in-reply-to");

        private String elementName;

        ElementTag(String elementName) {
            this.elementName = elementName;
        }

        @Override
        public String toString() {
            return this.elementName;
        }
    }

    private Map<String, Object> feed = new HashMap<String, Object>();
    private List<Map<String, String>> entries = new ArrayList<Map<String, String>>();

    private Map<String, String> currentEntry = null;
    private StringBuilder builder = null;

    public Map<String, Object> getFeed() {
        return this.feed;
    }

    @Override
    public void startDocument() throws SAXException {
        this.currentEntry = null;
        this.builder = null;
        this.entries.clear();
        this.feed.clear();
    }

    @Override
    public void endDocument() throws SAXException {
        this.feed.put("entries", this.entries);

        for (final Map<String, String> entry : this.entries) {
            if (log.isInfoEnabled()) {
                log.info(entry);
            }
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        this.builder = new StringBuilder();

        if (qName.equalsIgnoreCase(ElementTag.ENTRY.toString())) {
            // create a new instance of employee
            this.currentEntry = new HashMap<String, String>();
            return;
        }

        if (this.currentEntry == null) {
            return;
        }

        if (qName.equalsIgnoreCase(ElementTag.CATEGORY.toString())) {
            this.currentEntry.put("category", attributes.getValue("term"));
        } else if (qName.equalsIgnoreCase(ElementTag.LINK.toString())) {
            if (StringUtils.equalsIgnoreCase(attributes.getValue("rel"), "alternate")) {
                this.currentEntry.put("link", attributes.getValue("href"));
            }
        } else if (qName.equalsIgnoreCase(ElementTag.IN_REPLY_TO.toString())) {
            this.currentEntry.put("ref", attributes.getValue("ref"));
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (this.currentEntry == null) {
            return;
        }

        final String value = this.builder.toString();

        if (qName.equalsIgnoreCase(ElementTag.ENTRY.toString())) {
            // add it to the list
            this.entries.add(this.currentEntry);
        } else if (qName.equalsIgnoreCase(ElementTag.ID.toString())) {
            this.currentEntry.put("id", value);
        } else if (qName.equalsIgnoreCase(ElementTag.TITLE.toString())) {
            this.currentEntry.put("title", value);
        } else if (qName.equalsIgnoreCase(ElementTag.UPDATED.toString())) {
            this.currentEntry.put("updated", value);
        } else if (qName.equalsIgnoreCase(ElementTag.CONTENT.toString())) {
            this.currentEntry.put("content", value);
        } else if (qName.equalsIgnoreCase(ElementTag.EMAIL.toString())) {
            this.currentEntry.put("email", value);
        }
    }

    /**
     * Note: The characters() method is not guaranteed to give you the complete character content of a text element in
     * one pass - the full text may span buffer boundaries. You need to buffer the characters yourself between the start
     * and end element events.
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        this.builder.append(ch, start, length);
    }
}
